package com.shengqi.xinnongren.work.ui.user.activities;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.user.model.ToBeVipBean;
import com.shengqi.xinnongren.work.ui.user.presenter.ToBeVipPresenter;
import com.shengqi.xinnongren.work.ui.user.view.ToBeVipView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ToBeVipActivity extends BaseMvpActivity<ToBeVipView, ToBeVipPresenter> implements ToBeVipView {

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private ToBeVipAdapter mAdapter;
    private List<ToBeVipBean> mlist;

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_to_be_vip;
    }

    @Override
    protected void initView() {
        mlist = new ArrayList<>();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(Global.genRecyclerViewItemDecoration(this, 1, Color.parseColor("#EEEEEE")));
        mAdapter = new ToBeVipAdapter(R.layout.item_to_be_vip, mlist);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.getData();
    }

    /**
     * 确定支付
     */
    @OnClick(R.id.tv_confirm_pay)
    void confirmPay() {
        ReChargePayActivity.start(this, mlist.get(mAdapter.getChoosePosition()).getMoney(), 2);
    }

    @Override
    protected ToBeVipPresenter createPresenter() {
        return new ToBeVipPresenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingView();
    }

    @Override
    public void showToBeVipData(List<ToBeVipBean> list) {
        mlist.clear();
        mlist.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    class ToBeVipAdapter extends BaseQuickAdapter<ToBeVipBean, BaseViewHolder> {

        int mChoosePosition;

        public int getChoosePosition() {
            return mChoosePosition;
        }

        public ToBeVipAdapter(int layoutResId, @Nullable List<ToBeVipBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ToBeVipBean item) {

            helper.setText(R.id.item_name, item.getName());
            helper.setText(R.id.item_money, String.format("￥%s", item.getMoney()));

            int position = helper.getAdapterPosition();
            helper.setImageResource(R.id.item_iv, mChoosePosition == position ? R.mipmap.ic_check_checked : R.mipmap.ic_check_normal);

            helper.itemView.setOnClickListener(v -> {
                mChoosePosition = position;
                notifyDataSetChanged();
            });
        }
    }
}
